//
//  XZHMovieFormView.m
//  Piece
//
//  Created by dllo on 16/6/14.
//  Copyright © 2016年 Northeast Dianli University. All rights reserved.
//

#import "XZHMovieFormView.h"
#import "Masonry.h"
#import "XZHMovieDetailPhotoCollectionViewCell.h"
@interface XZHMovieFormView ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong) UILabel *movieFormLabel;

@property (nonatomic, strong) UIImageView *formImageView;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) UILabel *infoLabel;

@end

@implementation XZHMovieFormView{

    UILabel *firstLabel;
    UILabel *secondLabel;
    UILabel *thirdLabel;
    UILabel *fourthLabel;
    UILabel *fifthLabel;
    
    UIButton *infoBtn;
    UIButton *photoBtn;
    UIButton *keywordBtn;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = [UIColor colorWithRed:0.99 green:0.99 blue:1.00 alpha:1.00];
        _movieFormLabel = [[UILabel alloc] init];
        _movieFormLabel.text = @"一个电影表";
        _movieFormLabel.font = [UIFont systemFontOfSize:12];
        _movieFormLabel.textColor = [UIColor colorWithRed:0.62 green:0.62 blue:0.62 alpha:1.00];
        [self addSubview:_movieFormLabel];
        [_movieFormLabel makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(10);
            make.top.equalTo(self).offset(0);
            make.width.equalTo(100);
            make.height.equalTo(30);
        }];
        
        infoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [infoBtn setBackgroundImage:[UIImage imageNamed:@"actor_normal"] forState:UIControlStateNormal];
        [infoBtn addTarget:self action:@selector(didClickedInfoBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:infoBtn];
        [infoBtn makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-5);
            make.top.equalTo(self).offset(-3);
            make.width.equalTo(35);
            make.height.equalTo(35);
        }];
        

        photoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [photoBtn setBackgroundImage:[UIImage imageNamed:@"still_selected"] forState:UIControlStateNormal];
        [self addSubview:photoBtn];
        [photoBtn addTarget:self action:@selector(didClickedPhotoBtn:) forControlEvents:UIControlEventTouchUpInside];

        [photoBtn makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(infoBtn.left).offset(-5);
            make.top.equalTo(infoBtn);
            make.width.equalTo(35);
            make.height.equalTo(35);
        }];

//        keywordBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//        [keywordBtn setBackgroundImage:[UIImage imageNamed:@"plot_selected"] forState:UIControlStateNormal];
//        [keywordBtn addTarget:self action:@selector(didClickedKeywordBtn:) forControlEvents:UIControlEventTouchUpInside];
//        [self addSubview:keywordBtn];
//        [keywordBtn makeConstraints:^(MASConstraintMaker *make) {
//            make.right.equalTo(photoBtn.left).equalTo(-5);
//            make.top.equalTo(infoBtn);
//            make.width.equalTo(35);
//            make.height.equalTo(35);
//        }];


        UIView *currentView = [[UIView alloc] init];
        [self addSubview:currentView];
        [currentView makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_movieFormLabel.bottom);
            make.left.equalTo(self);
            make.width.equalTo(ScreenWidth);
            make.height.equalTo(110);
        }];
        

        
//        NSLog(@"array:%@", _model.keywords);
        self.formImageView = [[UIImageView alloc] init];
        _formImageView.image = [UIImage imageNamed:@"gross_border"];
        [currentView addSubview:_formImageView];
        [_formImageView makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(0, 0, 0, 0));
        }];
        
        firstLabel = [[UILabel alloc] init];
        firstLabel.textColor = [UIColor colorWithRed:0.52 green:0.52 blue:0.52 alpha:1.00];
        firstLabel.textAlignment = 1;
        firstLabel.font = [UIFont systemFontOfSize:12];
        [_formImageView addSubview:firstLabel];
        [firstLabel makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(48);
            make.width.equalTo(ScreenWidth / 3);
            make.left.equalTo(_formImageView).offset(5);
            make.top.equalTo(_formImageView).offset(5);
        }];
        
        secondLabel = [[UILabel alloc] init];
        secondLabel.textColor = [UIColor colorWithRed:0.52 green:0.52 blue:0.52 alpha:1.00];
        secondLabel.textAlignment = 1;
        secondLabel.font = [UIFont systemFontOfSize:12];
        [_formImageView addSubview:secondLabel];
        [secondLabel makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(48);
            make.width.equalTo(ScreenWidth / 3);
            make.left.equalTo(firstLabel.right);
            make.top.equalTo(firstLabel);
        }];
        
        thirdLabel = [[UILabel alloc] init];
        thirdLabel.textColor = [UIColor colorWithRed:0.52 green:0.52 blue:0.52 alpha:1.00];
        thirdLabel.textAlignment = 1;
        thirdLabel.font = [UIFont systemFontOfSize:12];
        [_formImageView addSubview:thirdLabel];
        [thirdLabel makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(48);
            make.width.equalTo(ScreenWidth / 3);
            make.left.equalTo(secondLabel.right);
            make.top.equalTo(firstLabel);
        }];
        
        
        fourthLabel = [[UILabel alloc] init];
        fourthLabel.textColor = [UIColor colorWithRed:0.52 green:0.52 blue:0.52 alpha:1.00];
        fourthLabel.textAlignment = 1;
        fourthLabel.font = [UIFont systemFontOfSize:12];
        [_formImageView addSubview:fourthLabel];
        [fourthLabel makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(48);
            make.width.equalTo(ScreenWidth / 2);
            make.left.equalTo(firstLabel);
            make.top.equalTo(firstLabel.bottom);
        }];
        
        fifthLabel = [[UILabel alloc] init];
        fifthLabel.textColor = [UIColor colorWithRed:0.52 green:0.52 blue:0.52 alpha:1.00];
        fifthLabel.textAlignment = 1;
        fifthLabel.font = [UIFont systemFontOfSize:12];
        [_formImageView addSubview:fifthLabel];
        [fifthLabel makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(48);
            make.width.equalTo(ScreenWidth / 2);
            make.left.equalTo(fourthLabel.right);
            make.top.equalTo(firstLabel.bottom);
        }];
        
        self.infoLabel = [[UILabel alloc] init];
        self.infoLabel.font = [UIFont systemFontOfSize:12];
        self.infoLabel.numberOfLines = 0;
        self.infoLabel.hidden = YES;
        [currentView addSubview:_infoLabel];
        [_infoLabel makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(0, 10, 0, 10));
        }];
        
        UICollectionViewFlowLayout *flowLayOut = [[UICollectionViewFlowLayout alloc] init];
        flowLayOut.itemSize = CGSizeMake(375 / 4 + 16, 110);
        flowLayOut.minimumInteritemSpacing = 0;
        flowLayOut.minimumLineSpacing = 2;
        flowLayOut.sectionInset = UIEdgeInsetsMake(0, 2, 0, 0);
        flowLayOut.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        //初始化collectionView
        self.collectionView = [[UICollectionView alloc] initWithFrame:currentView.frame collectionViewLayout:flowLayOut];
        self.collectionView.backgroundColor = [UIColor whiteColor];
        self.collectionView.delegate = self;
        self.collectionView.dataSource = self;
        self.collectionView.hidden = YES;
        self.collectionView.showsHorizontalScrollIndicator = NO;
        [currentView addSubview:self.collectionView];
        [self.collectionView registerNib:[UINib nibWithNibName:@"XZHMovieDetailPhotoCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"photoCell"];
        [_collectionView makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(0, 0, 0, 0));
        }];
        
        self.infoLabel.hidden = YES;
        self.formImageView.hidden = YES;
        self.collectionView.hidden = NO;
    }
    return self;
}



- (void)didClickedInfoBtn:(UIButton *)sender {
    self.movieFormLabel.text = @"演员";
    self.infoLabel.hidden = NO;
    self.formImageView.hidden = YES;
    self.collectionView.hidden = YES;
    [keywordBtn setBackgroundImage:[UIImage imageNamed:@"plot_normal"] forState:UIControlStateNormal];
    [photoBtn setBackgroundImage:[UIImage imageNamed:@"still_normal"] forState:UIControlStateNormal];
    [infoBtn setBackgroundImage:[UIImage imageNamed:@"actor_selected"] forState:UIControlStateNormal];
    
}

- (void)didClickedPhotoBtn:(UIButton *)sender {
    self.movieFormLabel.text = @"剧照";
    self.infoLabel.hidden = YES;
    self.formImageView.hidden = YES;
    self.collectionView.hidden = NO;
    [keywordBtn setBackgroundImage:[UIImage imageNamed:@"plot_normal"] forState:UIControlStateNormal];
    [photoBtn setBackgroundImage:[UIImage imageNamed:@"still_selected"] forState:UIControlStateNormal];
    [infoBtn setBackgroundImage:[UIImage imageNamed:@"actor_normal"] forState:UIControlStateNormal];
}

- (void)didClickedKeywordBtn:(UIButton *)sender {
    self.movieFormLabel.text = @"一个电影表";
    self.infoLabel.hidden = YES;
    self.formImageView.hidden = NO;
    self.collectionView.hidden = YES;
    [keywordBtn setBackgroundImage:[UIImage imageNamed:@"plot_selected"] forState:UIControlStateNormal];
    [photoBtn setBackgroundImage:[UIImage imageNamed:@"still_normal"] forState:UIControlStateNormal];
    [infoBtn setBackgroundImage:[UIImage imageNamed:@"actor_normal"] forState:UIControlStateNormal];
//    NSLog(@"%@", _model);
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _model.photo.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    XZHMovieDetailPhotoCollectionViewCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"photoCell" forIndexPath:indexPath];
    cell.photoUrl = [_model.photo objectAtIndex:indexPath.row];
    return cell;
}

- (void)setModel:(XZHMovieDetailModel *)model {
    if (_model != model) {
        _model = model;
        self.infoLabel.text = model.info;
        [_collectionView reloadData];
        NSArray *array = [_model.keywords componentsSeparatedByString:@";"];
        NSLog(@"%@",array);
        firstLabel.text = [array objectAtIndex:0];
        secondLabel.text = [array objectAtIndex:1];
        thirdLabel.text = [array objectAtIndex:2];
        fourthLabel.text = [array objectAtIndex:3];
        fifthLabel.text = [array objectAtIndex:4];
        
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
